package com.xbongbong.saas.script.tempenum.product;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.field.LinkKeyFieldInfoPojo;
import com.xbongbong.paas.field.LinkedAttrPoJo;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.SummaryTypeEnum;
import com.xbongbong.saas.enums.ForbiddenSettingEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CostAdjustEnum;
import com.xbongbong.saas.enums.business.OutstockEnum;
import com.xbongbong.saas.enums.product.CostAdjustProductEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * @author WuHB
 * @date 2021/11/18 09:35
 */
@Getter
public enum TempCostAdjustProductEnum {
    // 产品ID
    PRODUCT(CostAdjustProductEnum.PRODUCT),
    // 仓库
    WAREHOUSE(CostAdjustProductEnum.WAREHOUSE),
    // 基本单位
    BASIC_UNIT(CostAdjustProductEnum.BASIC_UNIT),
    // 批次
    BATCH(CostAdjustProductEnum.BATCH),
    // 生产日期
    PRODUCE_DATE(CostAdjustProductEnum.PRODUCE_DATE),
    //保质期
    GUARANTEE_PERIOD(CostAdjustProductEnum.GUARANTEE_PERIOD),
    //到期日期
    EXPIRE_DATE(CostAdjustProductEnum.EXPIRE_DATE),
    // 成本小计调整金额
    ADJUST_AMOUNT(CostAdjustProductEnum.ADJUST_AMOUNT),
    // 库存数量
    STOCK(CostAdjustProductEnum.STOCK),
    // 原产品成本（元）
    OLD_COST(CostAdjustProductEnum.OLD_COST),
    // 新产品成本（元）
    NEW_COST(CostAdjustProductEnum.NEW_COST),
    // 成本小计金额（元）
    TOTAL_COST(CostAdjustProductEnum.TOTAL_COST),

    ;
    private final CostAdjustProductEnum anEnum;

    TempCostAdjustProductEnum(CostAdjustProductEnum costAdjustProductEnum) {
        this.anEnum = costAdjustProductEnum;
    }

    public static List<TempCostAdjustProductEnum> getAllEnum() {
        List<TempCostAdjustProductEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    /**
     * 初始化关联产品信息
     *
     * @param fieldAttrEntity    表单字段属性实体类
     * @param linkedAttrPoJoList 关联字段pojo
     * @param linkedShowPoJoList 关联显示pojo
     * @param paasFormEntity     pass表单实体
     */
    public static void initProduct(FieldAttrEntity fieldAttrEntity, List<LinkedAttrPoJo> linkedAttrPoJoList, List<LinkedAttrPoJo> linkedShowPoJoList, PaasFormEntityExt paasFormEntity) {
        LinkInfoPojo linkInfoPojo = new LinkInfoPojo();
        linkInfoPojo.setLinkBusinessType(XbbRefTypeEnum.PRODUCT.getCode());
        linkInfoPojo.setLinkedAttr(linkedAttrPoJoList);
        linkInfoPojo.setLinkSaasMark(SaasMarkEnum.SAAS.getCode());
        linkInfoPojo.setLinkKey(linkedAttrPoJoList.get(0).getAttr());
        linkInfoPojo.setLinkFormId(paasFormEntity.getId());
        linkInfoPojo.setLinkMenuId(paasFormEntity.getMenuId());
        linkInfoPojo.setLinkAppId(paasFormEntity.getAppId());
        linkInfoPojo.setCondition(new ArrayList<>());
        LinkKeyFieldInfoPojo linkKeyFieldInfoPojo = new LinkKeyFieldInfoPojo();
        BeanUtil.copyProperties(linkedAttrPoJoList.get(0), linkKeyFieldInfoPojo);
        linkInfoPojo.setLinkKeyAttr(linkKeyFieldInfoPojo);
        linkInfoPojo.setLinkedShowAttr(linkedShowPoJoList);
        fieldAttrEntity.setLinkInfo(linkInfoPojo);
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_VISIBLE_PERMISSION.getAlias()));
    }

    /**
     * 初始化关联仓库
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initWarehouse(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.PRODUCT_WAREHOUSE.getCode());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(true);
        saasParticularAttributePoJo.setRelyAttr(CostAdjustProductEnum.PRODUCT.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.PRODUCT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_VISIBLE_CORRELATION_FIELD.getAlias()));
    }

    /**
     * 初始化关联仓库
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initBasicUnit(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_ITEMS.getAlias(),ForbiddenSettingEnum.DISABLE_RULE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    /**
     * 初始化批次
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initBatch(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setAmountFlag(0);
        fieldAttrEntity.setVisible(1);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setEditHide(0);
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setEditableAdvanceEnable(0);
        fieldAttrEntity.setFieldId(UUID.randomUUID().toString());
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(),
                ForbiddenSettingEnum.DISABLE_RULE.getAlias(),ForbiddenSettingEnum.DISABLE_TYPE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    /**
     * 初始化调整金额
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initAdjustAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        fieldAttrEntity.setIsForbidden(1);
        fieldAttrEntity.setAccuracy(5);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    /**
     * 初始化库存
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initStock(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setShowEditable(1);
    }

    /**
     * 初始化成本
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initOldCost(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        fieldAttrEntity.setIsForbidden(1);
        fieldAttrEntity.setAccuracy(5);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(), ForbiddenSettingEnum.DISABLE_VISIBLE_PERMISSION.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    /**
     * 初始化新成本
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initNewCost(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "DIVIDE({self." + CostAdjustEnum.PRODUCT.getAttr() + "." + CostAdjustProductEnum.TOTAL_COST.getAttr() + "},{self." + CostAdjustEnum.PRODUCT.getAttr() + "." + CostAdjustProductEnum.STOCK.getAttr() + "}, 7)";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + CostAdjustEnum.PRODUCT.getAttr() + "." + CostAdjustProductEnum.TOTAL_COST.getAttr(), "关联产品.成本小计");
        labelMap.put("self." + CostAdjustEnum.PRODUCT.getAttr() + "." + CostAdjustProductEnum.STOCK.getAttr(), "关联产品.库存");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setAccuracy(5);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        fieldAttrEntity.setIsForbidden(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(), ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(), ForbiddenSettingEnum.DISABLE_VISIBLE_PERMISSION.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);

    }

    /**
     * 初始化总成本
     *
     * @param fieldAttrEntity 表单字段属性实体类
     */
    public static void initTotalCost(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUM(MULTIPLY({self." + CostAdjustEnum.PRODUCT.getAttr() + "." + CostAdjustProductEnum.OLD_COST.getAttr() + "}," +
                "{self." + CostAdjustEnum.PRODUCT.getAttr() + "." + CostAdjustProductEnum.STOCK.getAttr() + "}, 7)," +
                "IF(OR(ISEMPTY({self." + CostAdjustEnum.PRODUCT.getAttr() + "." + CostAdjustProductEnum.ADJUST_AMOUNT.getAttr() + "}),{self." + CostAdjustEnum.PRODUCT.getAttr() + "." + CostAdjustProductEnum.ADJUST_AMOUNT.getAttr() + "}==0),0,{self." + CostAdjustEnum.PRODUCT.getAttr() + "." + CostAdjustProductEnum.ADJUST_AMOUNT.getAttr() + "}))";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + CostAdjustEnum.PRODUCT.getAttr() + "." + CostAdjustProductEnum.OLD_COST.getAttr(), "关联产品.原产品成本");
        labelMap.put("self." + CostAdjustEnum.PRODUCT.getAttr() + "." + CostAdjustProductEnum.STOCK.getAttr(), "关联产品.库存");
        labelMap.put("self." + CostAdjustEnum.PRODUCT.getAttr() + "." + CostAdjustProductEnum.ADJUST_AMOUNT.getAttr(), "关联产品.调整金额");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        fieldAttrEntity.setAccuracy(5);
        fieldAttrEntity.setIsForbidden(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(), ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(), ForbiddenSettingEnum.DISABLE_VISIBLE_PERMISSION.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);

    }


    public static void initProductDate(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setDateType("yyyy-MM-dd");
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }

    public static void initGuaranteePeriod(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setIntegerOnly(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias(),ForbiddenSettingEnum.DISABLE_RULE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setEditable(0);
    }
    public static void initExpireDate(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setDateType("yyyy-MM-dd");
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "{self." + OutstockEnum.PRODUCT.getAttr() + "." + SelectProductEnum.PRODUCE_DATE.getAttr() + "}+24*60*60*{self." + OutstockEnum.PRODUCT.getAttr() + "." + SelectProductEnum.GUARANTEE_PERIOD.getAttr() + "}";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + OutstockEnum.PRODUCT.getAttr() + "." + SelectProductEnum.PRODUCE_DATE.getAttr(), "关联产品.生产日期");
        labelMap.put("self." + OutstockEnum.PRODUCT.getAttr() + "." + SelectProductEnum.GUARANTEE_PERIOD.getAttr(), "关联产品.保质期");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
    }
}
